﻿
var CurrentType = 0;
var CurrentData = {};

Ext.onReady(function () {
    PageSafe();


    var docstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Flow.FlowView', 'GetDocInfo'),
        root: 'ResponseObject',
        fields: ['PID', 'DocName', 'DocPrice', 'DocCustom', 'IsGoods', 'GoodsCount', 'GoodsName', 'Remark', 'MoreInfo', 'UserID', 'DocState', 'DState', 'LastUpdate', 'NTime'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                re.params.info = {
                    SearchName: Ext.getCmp("searchname").getValue(),
                    SearchType: CurrentType
                };
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var docsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = docgrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentData = k[0].json;
                }
            },
            rowdeselect: function () {
                CurrentData = {};
            }
        }
    });
    function NumFormat(s, m) {
        m.css = 'RedNum';
        return s;
    }
    var docgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 500,
        sm: docsm,
        loadMask: true, //加载
        columns: [
       docsm,
       { id: 'DocName', header: '申请单名称', width: 100, dataIndex: 'DocName', sortable: true },
       { id: 'DocPrice', header: '价格', width: 60, dataIndex: 'DocPrice', sortable: true },
       { id: 'DocCustom', header: '客户名称', width: 100, dataIndex: 'DocCustom', sortable: true },
       { id: 'IsGoods', header: '是否物品', width: 80, dataIndex: 'IsGoods', sortable: true },
       { id: 'GoodsName', header: '物品名称', width: 80, dataIndex: 'GoodsName', sortable: true },
       { id: 'GoodsCount', header: '数量', width: 60, dataIndex: 'GoodsCount', sortable: true },
        { id: 'DState', header: '状态', width: 60, dataIndex: 'DState', sortable: true },
       { id: 'NTime', header: '日期', width: 100, dataIndex: 'NTime', sortable: true },
       { id: 'NTime', header: '查看', width: 100, dataIndex: '', sortable: true, renderer: function () {
           return '<a href="#" onclick="DownDoc();">下载查看</a>';
       } 
       }
   ],
        store: docstore
    });



    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '申请单填写',
            iconCls: 'document-icon',
            tbar: [{ text: '新增申请单', iconCls: 'add-icon', handler: function () {
                var addwin = new Ext.Window({
                    height: 300,
                    width: 400,
                    title: '新增广告申请单',
                    resizable: false,
                    modal: true,
                    iconCls: 'document-icon',
                    items: new Ext.form.FormPanel({
                        id: 'docadd',
                        enctype: 'multipart/form-data',
                        method: 'POST',
                        border: false,
                        bodyStyle: 'padding-top:20px',
                        fileUpload: true,
                        defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                        labelAlign: 'right',
                        items: [{
                            fieldLabel: '申请单名称',
                            width: 120,
                            id: 'DocName',
                            name: 'DocName'
                        }, {
                            fieldLabel: '价格',
                            width: 80,
                            id: 'DocPrice',
                            name: 'DocPrice'
                        }, {
                            fieldLabel: '客户名称',
                            width: 150,
                            id: 'DocCustom',
                            name: 'DocCustom'
                        }, {
                            fieldLabel: '是否物品',
                            xtype: 'checkbox',
                            id: 'isGood',
                            name: 'isGood'
                        }, {
                            fieldLabel: '物品名称',
                            width: 120,
                            id: 'GoodName',
                            name: 'GoodName'
                        }, {
                            fieldLabel: '物品数量',
                            width: 80,
                            id: 'GoodC',
                            name: 'GoodC'
                        }, {
                            fieldLabel: '说明',
                            width: 200,
                            id: 'Remark',
                            name: 'Remark'
                        }, {
                            fieldLabel: '附件上传',
                            width: 200,
                            id: 'Moreinfo',
                            name: 'Moreinfo',
                            inputType: 'file'
                        }]
                    }),
                    buttons: [{ text: '保存', handler: function () {
                        if (Ext.getCmp("DocName").getValue() == "") { Ext.Msg.alert("检查提示", "请输入申请单名称"); return; }
                        if (Ext.getCmp("DocPrice").getValue() == "") { Ext.Msg.alert("检查提示", "请输入申请单金额"); return; }
                        if (Ext.getCmp("DocCustom").getValue() == "") { Ext.Msg.alert("检查提示", "请输入客户名称"); return; }

                        Ext.getCmp("docadd").getForm().submit({
                            url: 'DocFile.aspx',
                            failure: function (form, action) { Ext.Msg.alert("提示", "申请单上报失败"); },
                            success: function (form, action) {
                                addwin.close();
                                docstore.load();
                            }
                        });
                    }
                    }, { text: '取消', handler: function () { addwin.close(); } }]
                }).show();
            }
            }, { text: '删除', iconCls: 'del-icon', handler: function () {
                if (CurrentData.PID == null) { Ext.Msg.alert("提示信息", "请选择需要删除的申请单"); return; }

                var re = new JsonRequest();
                re.params.info = CurrentData;
                Ext.Ajax.request({
                    url: Ext.getClassUrl('Flow.FlowView', 'DelDocInfo'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (!obj.HasError) {
                            Ext.Msg.alert("提示信息", "广告申请单删除成功");
                            docstore.load();
                        }
                        else {
                            Ext.Msg.alert("提示信息", "广告申请单删除失败，请联系管理员");
                        }
                    }
                });
            }
            },
             '-', '申请单名称/客户名称：', { xtype: 'textfield', width: 120, height: 23, id: 'searchname' }, { text: '查询', iconCls: 'search-icon', handler: function () { docstore.load(); } }, '->', '-',
            { text: '查询全部', iconCls: 'allsearch-icon', handler: function () { CurrentType = -1; docstore.load(); } },
            { text: '查询成功申请单', iconCls: 'passsearch-icon', handler: function () { CurrentType = 2; docstore.load(); } },
            { text: '查询审核中申请单', iconCls: 'passingsearch-icon', handler: function () { CurrentType = 1; docstore.load(); } },
            { text: '查询未审核申请单', iconCls: 'secretsearch-icon', handler: function () { CurrentType = 0; docstore.load(); } }, '-'],
            items: docgrid
        }],
        listeners: {
            render: function () {
                docstore.load();
            }
        }
    });
});

var DownDoc = function () {
    window.open("../" + CurrentData.MoreInfo);
}